package com.itheima.controller;


import com.itheima.bean.ShoppingCart;
import com.itheima.common.R;
import com.itheima.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RestController
@Transactional
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 查找当前用户的购物车里面的商品
     * @return
     */
    @GetMapping("/list")
    public R selectAll(){
        List<ShoppingCart> shoppingCarts = shoppingCartService.selectByUserId();
        return R.success(shoppingCarts);
    }

    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart){
        int row = shoppingCartService.insert(shoppingCart);

        if(row>0)
            return R.success("添加购物车成功！！！");
        else return R.error("添加购物车失败！！！");
    }

    /**
     * 清空当前用户的购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R deleteAll(){
        int row = shoppingCartService.delete();
        if(row>0)
            return R.success("清空购物车成功！！！");
        else return R.error("清空购物车失败！！！");
    }

    /**
     * 减少所选的商品数量
     * @return
     */
    @PostMapping("/sub")
    public R sub(@RequestBody ShoppingCart shoppingCart){
        int sub = shoppingCartService.sub(shoppingCart);
        if(sub>0)
            return R.success("减少商品成功！！！");
        else return R.error("减少商品失败！！！");
    }








}
